package com.lbj.mvvm.ext

import android.content.Context
import android.content.pm.PackageManager

/**
 * 作者：李炳军
 * 创建日期：2021-04-01  11:19
 * 释义：
 */
/**
 * 获取版本名称
 */
fun Context.getVersionName(): String {
    val manager = packageManager
    var versionName = ""
    try {
        val packageInfo =
            manager.getPackageInfo(packageName, 0)
        versionName = packageInfo.versionName
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return versionName
}

/**
 * 获取版本号
 */
fun Context.getVersionCode(): Int {
    val manager = packageManager
    try {
        val packageInfo =
            manager.getPackageInfo(packageName, 0)
        return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            packageInfo.longVersionCode.toInt()
        } else {
            packageInfo.versionCode
        }
    } catch (e: PackageManager.NameNotFoundException) {
        e.printStackTrace()
    }
    return -1
}